﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESMAM.Model.SYS;
using ESMAM.DAL.SYS;
using System.Collections.Specialized;

namespace ESMAM.BLL.SYS
{
    public class MenuBLL
    {

        private static MenuDAL menuDal = new MenuDAL();

        static MenuBLL()
        {
            RefreshAllMenu();
        }

        /// <summary>
        /// 所有菜单项
        /// </summary>
        public static List<Tree> LST_ALL_MENU
        {
            get;
            private set;
        }

        /// <summary>
        /// 刷新获取 设置所有菜单项
        /// </summary>
        public static void RefreshAllMenu()
        {
            LST_ALL_MENU = menuDal.GetAllMenu().OrderByDescending(t => t.score).ToList();
        }


        public static Retful GetMainMenu(sys_user currentUser)
        {
            List<Tree> lstMenu = null;
            try
            {
                //二级菜单 && 启用状态
                lstMenu = LST_ALL_MENU.Where(t => t.level == 1 && t.enabled == 1).OrderByDescending(t => t.score).ToList();

                return Retful.CreateSuccess(lstMenu);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }

        public static Retful GetChildMenu(string pid, sys_user currentUser)
        {
            List<Tree> lstTree = null;
            try
            {
                //过滤后克隆，防止修改掉原有 数据集合
                lstTree = LST_ALL_MENU.Where(t => t.enabled == 1).ToList().Clone().Where(t => t.pid == pid).ToList();

                foreach (var tree in lstTree)
                {
                    //递归设置每一个子节点的children集合
                    DiGuiTree(tree);
                }

                lstTree = lstTree.OrderByDescending(t => t.score).ToList();
                
                return Retful.CreateSuccess(lstTree);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }

        private static void DiGuiTree(Tree defTree)
        {
            if (defTree == null)
            {
                return;
            }
            //过滤后克隆，防止修改掉原有 数据集合
            var lstWhere = LST_ALL_MENU.Where(t => t.enabled == 1).ToList().Clone().Where(t => t.pid == defTree.id).ToList();
            if (lstWhere.Any())
            {
                for (int j = 0; j < lstWhere.Count; j++)
                {
                    Tree trTemp = lstWhere[j];
                    DiGuiTree(trTemp);
                }
                defTree.children = lstWhere;
            }
            else
            {
                defTree.children = null;
            }
        }

        public static Retful UpsertTree(NameValueCollection form, sys_user user)
        {
            Retful ret = null;
            try
            {
                ret = menuDal.Upsert(new NameValueCollection(form), user);

                //刷新缓存的菜单项
                if (ret.Code == 1)
                {
                    RefreshAllMenu();
                }
                return ret;
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }

        private static void DiGuiGetMenuIds(string id, ref List<string> lstIds)
        {
            List<Tree> lstTree = LST_ALL_MENU.Where(t => t.pid == id).ToList();
            if (lstTree.Any())
            {
                for (int j = 0; j < lstTree.Count; j++)
                {
                    Tree trTemp = lstTree[j];
                    lstIds.Add(trTemp.id);
                    DiGuiGetMenuIds(trTemp.id, ref lstIds);
                }
            }
            else
            {
                lstIds.Add(id);
            }
        }


        public static Retful DelMenu(string[] ids)
        {
            try
            {
                Tree topTree = LST_ALL_MENU.Where(t => ids.Contains(t.id)).Where(t => t.level == 0).FirstOrDefault();
                if (topTree != null)
                {
                    return Retful.CreateFail("顶级菜单不可以删除！");
                }

                for (int j = 0; j < ids.Length; j++)
                {
                    Tree treeDef = LST_ALL_MENU.Where(t => t.id == ids[j]).FirstOrDefault();
                    DiGuiTree(treeDef);
                    if (treeDef.children != null && treeDef.children.Any())
                    {
                        return Retful.CreateFail("要删除的菜单中，存在子菜单的情况，请先删除子菜单！");
                    }
                }
                int res = menuDal.DelMenu(ids);

                //刷新缓存的菜单项
                if (res > 0)
                {
                    RefreshAllMenu();
                }

                return Retful.CreateSuccess(null, "删除成功！");
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }
    }
}
